package com.hmys.api.controller;

import com.github.pagehelper.PageInfo;
import com.hmys.api.model.WebFriendLink;
import com.hmys.api.response.Result;
import com.hmys.api.response.ResultCode;
import com.hmys.api.response.ResultPage;
import com.hmys.api.service.WebFriendLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/03/26.
 */
@RestController
@Api(tags = "友情链接")
@RequestMapping(value = "/friend/link")
public class FriendLinkController {
    @Autowired
    private WebFriendLinkService webFriendLinkService;

    @ApiOperation(value = "列表", notes = "列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "type", value = "链接类型（0:所有,1:文本,2:图片）", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageIndex", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页数", required = true, paramType = "path")
    })
    @GetMapping(value = "/list/{type}/{pageIndex}/{pageSize}")
    public Object list(@PathVariable("type") Long type,
                       @PathVariable("pageIndex") Integer pageIndex,
                       @PathVariable("pageSize") Integer pageSize) {
        WebFriendLink record = new WebFriendLink();
        if (!StringUtils.isEmpty(type) && type > 0) {
            if (type == 1) {
                record.setLinkType("text");
            } else if (type == 2) {
                record.setLinkType("img");
            } else {

            }
        }
        record.setStatus(true);
        record.setSort(1);
        PageInfo<WebFriendLink> pageInfo = webFriendLinkService.selectByBean(pageIndex, pageSize, record);
        if (pageInfo.getList().size() > 0) {
            return new ResultPage(ResultCode.SUCCESS, pageInfo);
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }

    @ApiOperation(value = "详情", notes = "详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int", name = "id", value = "友情链接ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/detail/{id}")
    public Object detail(@PathVariable("id") Integer id) {
        if (!StringUtils.isEmpty(id) && id < 1) {
            return new Result<>(ResultCode.ERROR_PARAM);
        }
        WebFriendLink record = webFriendLinkService.selectByPrimaryKey(id);
        if (record != null) {
            if (record.getStatus()) {
                return new Result<>(ResultCode.SUCCESS, record);
            } else {
                return new Result<>(ResultCode.HAVE_DELETED);
            }
        } else {
            return new Result<>(ResultCode.NOT_FOUND);
        }
    }
}
